package com.coolwen.service;

import com.coolwen.dao.ChallengeMapper;
import com.coolwen.entity.Challenge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 松红 on 2016/12/10.
 */
@Service("challengeService")
public class ChallengeServiceImpl implements ChallengeService {
    @Autowired
    ChallengeMapper challengeMapper;

    /**
     * 通过确认状态码进行查找已经确认约战的记录
     *
     * @param isConfirm
     * @return
     */


    @Override
    public List<Challenge> findByConfirmState(String isConfirm, Integer teamId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("challengeIsconfirm", isConfirm);
        params.put("challengeTeamid", teamId);
        return challengeMapper.selectByConfirmState(params);
    }


    /**
     * 通过ReceiveId查找约战记录结束信息
     * @param isConfirm
     * @param ReceiveId
     * @return
     */

    @Override
    public List<Challenge> findChallengeByReceiveId(String isConfirm, Integer ReceiveId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("challengeIsconfirm", isConfirm);
        params.put("challengeReceiveid", ReceiveId);
        return challengeMapper.selectReceiveId(params);
    }

    /**
     * 通过teamId修改结束状态码
     * @param result
     * @param challengeId
     * @return
     */

    @Override
    public int updateChallengeResultByChallengeId(String result, Integer challengeId) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("challengeResult", result);
        params.put("challengeId", challengeId);
        return challengeMapper.updateResult(params);
    }


    /**
     * 通过teamId查找发起者的id(sendId)
     *
     * @param teamId
     * @return
     */
    @Override
    public List<Challenge> findSendIdByTeamId(Integer teamId) {
        return challengeMapper.selectSendId(teamId);
    }

    /**
     * 通过约战同意状态码进行查找待确认的约战记录
     *
     * @param AgreeState
     * @return
     */
    @Override
    public List<Challenge> findByAgreeState(Integer AgreeState) {
        return challengeMapper.selectByAgreeState(AgreeState);
    }

    /**
     * 通过challengeId来修改确认状态码
     *
     * @param challengeId
     * @return
     */
    @Override
    public int updateIsConfrimById(Integer challengeId) {
        return challengeMapper.updateIsConfrimById(challengeId);
    }

    /**
     * 通过前端提交约战表然后向数据库存入数据
     *
     * @param challenge
     * @return
     */

    @Override
    public int addChallenge(Challenge challenge,Integer sendId,Integer receiveId,String sendName,String receiveName) {

        challenge.setChallengeSendid(sendId);
        challenge.setChallengeTeamid(sendId);
        challenge.setChallengeReceiveid(receiveId);
        challenge.setChallengeHomescore(Short.valueOf("0"));
        challenge.setChallengeAwayscore(Short.valueOf("0"));
        challenge.setChallengeResult(Short.valueOf("0"));
        challenge.setChallengePresent(Short.valueOf("0"));
        challenge.setChallengeIsconfirm(Short.valueOf("3"));
        challenge.setChallengeIsagree(Short.valueOf("0"));
        challenge.setChallengeIshome(Short.valueOf("0"));
        challenge.setChallengeSendname(sendName);
        challenge.setChallengeReceivename(receiveName);

        return challengeMapper.insertSelective(challenge);
    }

    /**
     * 更改确认状态码为1
     *
     * @param challengeId
     * @return
     */
    @Override
    public int updateAgree(Integer challengeId) {
        return challengeMapper.updateAgree(challengeId);
    }

    /**
     * 更改同意状态码为2
     *
     * @param challengeId
     * @return
     */
    @Override
    public int updateAgreeAndConfirm(Integer challengeId) {
        return challengeMapper.updateAgreeAndConfrim(challengeId);
    }

    /**
     * 更改同意状态码为3
     *
     * @param challengeId
     * @return
     */
    @Override
    public int updateAgainst(Integer challengeId) {
        return challengeMapper.updateAgainst(challengeId);
    }


    /**
     * 插入主客场分数
     *并修改本场的录入比分状态码
     * @param challengeScore
     * @return
     */

    @Override
    public int updateGameScore(Challenge challengeScore,Integer challengeId) {

        Challenge challenge=findChallengeById(challengeId);
        challenge.setChallengeHomescore(challengeScore.getChallengeHomescore());
        challenge.setChallengeAwayscore(challengeScore.getChallengeAwayscore());
        challenge.setChallengePresent(challengeScore.getChallengePresent());
        return challengeMapper.updateByPrimaryKeySelective(challenge);
    }


    /**
     * 修改已结束录入比分状态码
     * @param challengePresent
     * @return
     */

    @Override
    public int updateChallengePresent(Challenge challengePresent) {
        return challengeMapper.updateByPrimaryKeySelective(challengePresent);
    }

    /**
     * 将确认状态码改为2
     *
     * @param challengeId
     * @return
     */
    @Override
    public int updateEnd(Integer challengeId) {
        return challengeMapper.updateEnd(challengeId);
    }

    /**
     * 将目前约战记录的状态码改为1
     *
     * @param challengeId
     * @return
     */
    @Override
    public int updateEndAndPresent(Integer challengeId) {
        return challengeMapper.updateEndAndPresent(challengeId);
    }

    /**
     * 将确认状态码改为4
     * @param challengeId
     * @return
     */
    @Override
    public int updateRefuse(Integer challengeId) {
        return challengeMapper.updateRefuse(challengeId);
    }

    @Override
    public Challenge findChallengeById(Integer challengeId) {
        return challengeMapper.selectByPrimaryKey(challengeId);
    }


}
